package com.wss.module.main.ui.view;

import com.wss.common.utils.ColorUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * Describe：千里之行始于足下，点滴汇聚成河
 * 波浪View
 * Created by 吴天强 on 2017/8/30.
 */
public class WaveView extends Component implements Component.EstimateSizeListener,
        Component.DrawTask {

    public static final String MODE_TRIANGLE = "triangle";
    public static final String MODE_CIRCLE = "circle";

    private String mWaveMode = MODE_TRIANGLE;
    private int mWaveCount;
    /**
     * 波浪的宽度
     */
    private int mWaveWidth;
    /**
     * 波浪的高度
     */
    private float mWaveHeight;

    private int mWidth;//View宽度
    private int mHeight;//View高度
    private float mRectWidth;//矩阵高=宽度
    private float mRectHeight;//矩阵高度

    private Paint mPaint;
    private RectFloat mRectF;
    private Path mPath;

    private float left;
    private float top;
    private float right;
    private float bottom;

    public WaveView(Context context) {
        this(context, null);
    }

    public WaveView(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public WaveView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        mWaveCount = attrSet.getAttr("waveCount").isPresent() ?
                attrSet.getAttr("waveCount").get().getIntegerValue() : 20;
        mWaveWidth = attrSet.getAttr("waveWidth").isPresent() ?
                attrSet.getAttr("waveWidth").get().getIntegerValue() : 20;
        mWaveMode = attrSet.getAttr("waveMode").isPresent() ?
                attrSet.getAttr("waveMode").get().getStringValue() : MODE_CIRCLE;
        Color mWaveColor = attrSet.getAttr("waveColor").isPresent() ?
                attrSet.getAttr("waveColor").get().getColorValue() : Color.BLACK;

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setColor(mWaveColor);
        //抗锯齿
        mPaint.setAntiAlias(true);
        mPath = new Path();

        addDrawTask(this);
        setEstimateSizeListener(this);


    }

    @Override
    public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {

        //测量宽度
        int specWidthMode = EstimateSpec.getMode(widthMeasureSpec);
        int specSize = EstimateSpec.getSize(widthMeasureSpec);

        if (specWidthMode == EstimateSpec.PRECISE) {
            mWidth = specSize;
        } else {
            if (specWidthMode == EstimateSpec.NOT_EXCEED) {
                //如果设置为wrap_content就默认给它一个值
                mWidth = 600;
            }
        }

        int specHeightMode = EstimateSpec.getMode(heightMeasureSpec);
        specSize = EstimateSpec.getSize(heightMeasureSpec);

        if (specHeightMode == EstimateSpec.PRECISE) {
            mHeight = specSize;
        } else {
            if (specHeightMode == EstimateSpec.NOT_EXCEED) {
                //如果设置为wrap_content就默认给它一个值
                mHeight = 550;
            }
        }

        //矩阵的宽高为View宽度的0.7倍
        mRectWidth = (float) ((mWidth - getPaddingLeft() - getPaddingRight()) * 0.7);
        mRectHeight = (float) ((mHeight - getPaddingLeft() - getPaddingRight()) * 0.7);

        //计算出每个波浪的高度，这样保证波浪与view两边你的绝对融合
        mWaveHeight = mRectHeight / mWaveCount;


        //计算出矩阵的位置让它显示在View的中间
        left = (mWidth - mRectWidth) / 2;
        top = (mHeight - mRectHeight) / 2;
        right = (mWidth - mRectWidth) / 2 + mRectWidth;
        bottom = (mHeight - mRectHeight) / 2 + mRectHeight;
        mRectF = new RectFloat(left, top, right, bottom);

        setEstimatedSize(EstimateSpec.getSizeWithMode(mWidth, specWidthMode),
                EstimateSpec.getSizeWithMode(mHeight, specHeightMode));

        return true;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(ColorUtils
                .colorToRgbColor(Color.LTGRAY.getValue()));
        setBackground(shapeElement);
        canvas.drawRect(mRectF, mPaint);
        if (mWaveMode == MODE_TRIANGLE) {
            //尖角型的
            float startX = left;
            float startY = top;
            mPath.moveTo(startX, startY);
            for (int i = 0; i < mWaveCount; i++) {
                //先画左边
                mPath.lineTo(startX - mWaveWidth, startY + mWaveHeight / 2 + i * mWaveHeight);
                mPath.lineTo(startX, startY + (i + 1) * mWaveHeight);
            }
            canvas.drawPath(mPath, mPaint);

            startX = left + mRectWidth;
            mPath.moveTo(startX, startY);
            for (int i = 0; i < mWaveCount; i++) {
                //再右边
                mPath.lineTo(startX + mWaveWidth, startY + mWaveHeight / 2 + i * mWaveHeight);
                mPath.lineTo(startX, startY + (i + 1) * mWaveHeight);
            }
            canvas.drawPath(mPath, mPaint);
        } else {

            //圆角型的
            for (int i = 0; i < mWaveCount; i++) {
                RectFloat mWaveRectF = new RectFloat(left - mWaveWidth / 2, top + i * mWaveHeight,
                        left + mWaveWidth / 2, top + (i + 1) * mWaveHeight);
                Arc arc = new Arc( 90, 180, true);
                canvas.drawArc(mWaveRectF, arc, mPaint);
            }
            for (int i = 0; i < mWaveCount; i++) {
                RectFloat mWaveRectF = new RectFloat(left + mRectWidth - mWaveWidth / 2, top + i * mWaveHeight,
                        left + mRectWidth + mWaveWidth / 2, top + (i + 1) * mWaveHeight);
                Arc arc = new Arc(270, 180, true);
                canvas.drawArc(mWaveRectF, arc, mPaint);
            }
        }
    }
}
